package com.example.demo.util;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiModelProperty;

/**
 * @author zhou
 *         + Result 处理方法
 */
public class R extends JSONObject {
  @ApiModelProperty(value = "code")
  private int code = ResultCode.SUCCESS.getCode();

  R() {
    this.put("code", code);
  }

  R(int code) {
    this.put("code", code);
  }

  R(int code, String message) {
    this.put("code", code);
    this.put("message", message);
  }

  R(String message) {
    this.put("message", message);
  }

  R(Object obj) {
    this.put("code", this.code).put("data", obj);
  }

  public static R success() {
    return new R(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
  }

  public static R success(String msg) {
    return R.success().put("message", msg);
  }

  public static R success(Object obj) {
    return R.success().put("data", obj);
  }

  public static R success(String msg, Object obj) {
    return new R(msg).put("data", obj);
  }

  /**
   * 服务器错误
   * 
   * @return
   */
  public static R error() {
    return new R(ResultCode.SERVERERROR.getCode());
  }

  public static R error(String msg) {
    return R.error().put("message", msg);
  }

  public static R serveError() {
    return new R(ResultCode.SERVERERROR.getCode(), ResultCode.SERVERERROR.getMessage());
  }

  /**
   * 操作失败
   * 
   * @return
   */
  public static R fail() {
    return new R(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMessage());
  }

  public static R fail(String msg) {
    return R.fail().put("message", msg == null ? "操作失败" : msg);
  }

  @Override
  public R put(String arg0, Object arg1) {
    super.put(arg0, arg1);
    return this;
  }
}
